﻿using UnityEngine;
using System.Collections;

public class KiritoMovement : StateMachineBehaviourEx 
{
    public Vector2 velocity = new Vector2(10, 0);
    public float gravity = 0.5f;
    public float heightJump = -6.0f;


    Vector2 newPosition = new Vector2(0, 0);
    Vector3 localScale;
    bool isJump = false;

    Animator animator;

    public enum KiritoStates
    {
        Idle,
        Run,
        Jump
    }

    void Start()
    {
        localScale = transform.localScale;
        animator = GetComponent<Animator>();
        currentState = KiritoStates.Idle;
    }

    #region Idle
    void Idle_EnterState()
    {
        isJump = false;
    }

    void Idle_Update()
    {
        if (Input.GetButton("left") || Input.GetButton("right"))
            currentState = KiritoStates.Run;

        if (Input.GetButtonDown("jump"))
            currentState = KiritoStates.Jump;
    }
    #endregion

    #region Run
    void Run_EnterState()
    {
        if (Input.GetButton("left") && localScale.x > 0)
        {
            localScale.x *= -1.0f;
            transform.localScale = localScale;
        }
        else if (Input.GetButton("right") && localScale.x < 0)
        {
            localScale.x *= -1.0f;
            transform.localScale = localScale;
        }
        animator.SetBool("run", true);
    }

    void Run_Update()
    {
        if (Input.GetButtonUp("left") || Input.GetButtonUp("right"))
            currentState = KiritoStates.Idle;

        if (Input.GetButton("left"))
        {
            newPosition.x -= velocity.x * Time.deltaTime;
            transform.position = newPosition;
        }

        if (Input.GetButton("right"))
        {
            newPosition.x += velocity.x * Time.deltaTime;
            transform.position = newPosition;
        }

        if (Input.GetButtonDown("jump"))
            currentState = KiritoStates.Jump;
    }

    void Run_ExitState()
    {
        animator.SetBool("run", false);
    }
    #endregion

    #region Jump
    void Jump_EnterState()
    {
        if (!isJump)
            velocity.y = -12f;
        animator.SetBool("jump", true);
    }

    void Jump_Update()
    {
        if (Input.GetButtonUp("jump"))
        {
            if (velocity.y < heightJump)
                velocity.y = heightJump;
        }

        if (Input.GetButton("left"))
        {
            if (localScale.x > 0.0f)
            {
                localScale.x *= -1.0f;
                transform.localScale = localScale;
            }
            newPosition.x -= velocity.x * Time.deltaTime;
        }
        else if (Input.GetButton("right"))
        {
            if (localScale.x < 0.0f)
            {
                localScale.x *= -1.0f;
                transform.localScale = localScale;
            }
            newPosition.x += velocity.x * Time.deltaTime;
        }

        velocity.y += gravity;
        newPosition.y -= velocity.y * Time.deltaTime;

        if (newPosition.y < 0)
        {
            newPosition.y = 0;
            velocity.y = 0.0f;
            currentState = KiritoStates.Idle;
        }

        transform.position = newPosition;
    }

    void Jump_ExitState()
    {
        animator.SetBool("jump", false);
    }
    #endregion

}
